/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sonth.DAL;

import sonth.Utils.JdbcConnector;
import sonth.EL.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 *
 * @author Administrator
 */
public final class UserDAO {

    /**
     * desc.
     */
    private UserDAO() {
    }

    /**
     * desc.
     * @param email desc
     * @param password desc
     * @return User desc
     * @throws Exception desc
     */
    public static User selectByEmailAndPassword(final String email,
            final String password) throws Exception {
        User user = null;

        Connection con = JdbcConnector.open();
        String sql = "SELECT * FROM [User] WHERE Email=? AND Password=?";
        PreparedStatement pst = con.prepareStatement(sql);
        int index = 1;
        pst.setString(index++, email);
        pst.setString(index++, password);
        ResultSet rs = pst.executeQuery();
        if (rs.next()) {
            String role = rs.getString("Role");
            user = new User(email, password, role);
        }
        JdbcConnector.close(con);

        return user;
    }
}
